package IBBB.network.sendmessage;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.Map;

public class send_update_user_schedule extends send_String {
    private Map<String, String> map = new LinkedHashMap<>();
    {
        map.put("四级真题词汇","cet4");
        map.put("六级真题词汇","cet6" );
        map.put("考研英语基础","kaoyan" );
        map.put("托福词汇","toefl");
        map.put("雅思词汇", "ielts");
    }
    public send_update_user_schedule(String ip, String dicName, String userName, Integer newSche) {
        super(ip);
        target_message = "updateUserSchedule#" + map.get(dicName) + "#" + userName + "#" + String.valueOf(newSche);
    }

    @Override
    public Void call() {

        Socket client = new Socket();
        try {
            client.connect(message_socket, 3000);
        } catch (Exception e) {
            updateMessage("timeout");
            return null;
        }

        try (ObjectOutputStream write = new ObjectOutputStream(client.getOutputStream())) {
            try (ObjectInputStream reader = new ObjectInputStream(client.getInputStream())) {
                write.writeObject(target_message);
                String res = (String)reader.readObject();
                client.close();
                if (res.equals("TRUE")) {
                    updateMessage("success");
                } else {
                    updateMessage("wrong");
                }
            }
        } catch (Exception e) {
            updateMessage("wrong");
        }
        return null;
    }
}
